package com.lw.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.edu.entity.Teacher;
import com.lw.edu.entity.vo.TeacherQuery;
import com.lw.edu.mapper.TeacherMapper;
import com.lw.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author lw
 * @since 2021-06-12
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    //条件带分页
    @Override
    public IPage<Teacher> pageList(Page<Teacher> page, TeacherQuery teacherQuery) {
        //构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty( teacherQuery.getName() )){
            wrapper.like( "name",teacherQuery.getName() );
        }
        if (!StringUtils.isEmpty( teacherQuery.getLevel() )){
            wrapper.eq( "level",teacherQuery.getLevel() );
        }
        if (!StringUtils.isEmpty( teacherQuery.getBegin() ))
            wrapper.ge( "gmt_create",teacherQuery.getBegin() ); //自动转换为时间格式进行比较
        if (!StringUtils.isEmpty( teacherQuery.getEnd() )){
            wrapper.le( "gmt_create",teacherQuery.getEnd() );
        }
        //排序
        wrapper.orderByDesc( "gmt_create" );
        IPage<Teacher> teacherIPage = baseMapper.selectPage( page, wrapper );
        return teacherIPage;
    }

    //前端自定义分页组件
    @Override
    public Map<String, Object> getPageList(Page<Teacher> pageTeacher) {
        Page<Teacher> pageParam = baseMapper.selectPage( pageTeacher, null );
        List<Teacher> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }
}
